﻿
; (function ($) {
    $.fn.extend({
        Tabs: function (options) {
            // 处理参数
            options = $.extend({
                tabObj:null,//包含选项卡所有内容的父div
                menuObj:null,//选项卡ul
                currentClass:'current',
                hideClass:'tdbao_list_hide',
                event: 'mouseover',
                timeout: 0,
                auto: 0,
                callback: null
            }, options);

            var self = $(this);
			var	tabBox =options.tabObj==null?self.children('div.tdbao_list_box').children('div'):options.tabObj.children('div');
			var	menu = options.menuObj==null?self.children('ul.tdbao_list_tab'):options.menuObj;
			var	items = menu.find('li');
			var	timer;

            var tabHandle = function (elem) {
                elem.siblings('li')
						.removeClass(options.currentClass)
						.end()
						.addClass(options.currentClass);

                tabBox.siblings('div')
						.addClass(options.hideClass)
						.end()
						.eq(elem.index())
						.removeClass(options.hideClass);
            },

				delay = function (elem, time) {
				    time ? setTimeout(function () { tabHandle(elem); }, time) : tabHandle(elem);
				},

				start = function () {
				    if (!options.auto) return;
				    timer = setInterval(autoRun, options.auto);
				},

				autoRun = function () {
				    var current = menu.find('li.'+options.currentClass),
						firstItem = items.eq(0),
						len = items.length,
						index = current.index() + 1,
						item = index === len ? firstItem : current.next('li'),
						i = index === len ? 0 : index;

				    current.removeClass(options.currentClass);
				    item.addClass(options.currentClass);

				    tabBox.siblings('div')
						.addClass(options.hideClass)
						.end()
						.eq(i)
						.removeClass(options.hideClass);
				};

            items.bind(options.event, function () {
                delay($(this), options.timeout);
                if (options.callback) {
                    options.callback(self);
                }
            });

            if (options.auto) {
                start();
                self.hover(function () {
                    clearInterval(timer);
                    timer = undefined;
                }, function () {
                    start();
                });
            }

            return this;
        }
    });
})(jQuery);